import { Modal, Space, message, Dropdown, Menu, Button } from "antd";

import { DownOutlined, FileTextOutlined, BarsOutlined, CloudDownloadOutlined, CloudUploadOutlined } from "@ant-design/icons";
import { useState, forwardRef, useImperativeHandle, useCallback, useEffect } from "react";
import SelectActions from "@/views/components/SelectActions";
import { getGoodsSkuLogPage, getGoodsSpuLogPage } from "@/pages/GoodsModule/shopGoods/service";
import useLogColumns from "./logColumns";
import ViewLogButton from "@/views/components/ViewLogButton";
import IconFont from "@/components/IconFont";

const RightBtns = (props, ref) => {
	const { query, selectedRows, isSkuType, handleCommand, dataSource } = props;

	const isCheck = useCallback(
		fn => {
			if (selectedRows.length > 0) {
				fn && fn(selectedRows);
			} else {
				message.warning("请先勾选一行数据");
			}
		},
		[selectedRows]
	);
	const isCheckStore = useCallback(
		fn => {
			isCheck(() => {
				const ecStoreIds = selectedRows.map(e => e.ecStoreId);
				const uniqueEcStoreIds = Array.from(new Set(ecStoreIds));
				if (uniqueEcStoreIds.length > 1) {
					message.warning("仅支持导出一个店铺的商品");
				} else {
					fn && fn(selectedRows);
				}
			});
		},
		[selectedRows]
	);
	const [actionType, setActionType] = useState([]);

	const { columns, isSkuType: logIsSkuType, actionhouse } = useLogColumns(actionType);
	return (
		<Space>
			{isSkuType && (
				<SelectActions
					onClick={handleCommand}
					options={[
						{
							value: "Excel导入",
							label: "Excel导入更新店铺商品",
							onClick: () => {
								{
									/* 如果页面没有数据，点击模板导出的时候需要提示‘请先筛选列表数据’ 
								传入 dataSource 用来判断页面是否有数据*/
								}
								handleCommand("Excel导入", {
									createTaskData: {
										businessParam: JSON.stringify({
											skuParam: query,
											businessKey: 2
										}),
										taskType: 3001
									},
									dataSource
								});
							}
						},
						{
							value: "线上导入",
							label: "线上导入店铺商品",
							onClick: () => {
								handleCommand("线上导入", {});
							}
						}
					]}
				>
					<CloudUploadOutlined className="main-color" />
					导入
				</SelectActions>
			)}
			{isSkuType && (
				<SelectActions
					options={[
						{
							label: "导出所选店铺商品资料",
							onClick: () => {
								isCheckStore(() => {
									handleCommand("导出所选店铺商品资料", {
										businessParam: JSON.stringify({
											ecStoreIds: [selectedRows[0].ecStoreId],
											businessKey: 1
										}),
										taskType: 3002
									});
								});
							}
						},
						{
							label: "导出筛选后的商品资料",
							onClick: () => {
								handleCommand("导出筛选后的商品资料", {
									businessParam: JSON.stringify({
										skuParam: query,
										businessKey: 2
									}),
									taskType: 3002
								});
							}
						},
						{
							label: "导出已勾选的商品资料",
							onClick: () => {
								isCheck(() => {
									handleCommand("导出筛选后的商品资料", {
										businessParam: JSON.stringify({
											ids: selectedRows.map(e => e.id),
											businessKey: 3
										}),
										taskType: 3002
									});
								});
							}
						}
					]}
				>
					<CloudDownloadOutlined className="main-color" />
					导出
				</SelectActions>
			)}

            <Button
                type="text"
                onClick={() => {
					handleCommand("自动生成设置");
                }}
            >
                <IconFont type="iconzidongshengchengshezhi" />
                自动生成设置
            </Button>

			<ViewLogButton
				tableKey={logIsSkuType ? "SKULogTable" : "SPULogTable"}
				columns={columns}
				width="1154px"
				height="544px"
				tableHeight={460}
				tableRequest={logIsSkuType ? getGoodsSkuLogPage : getGoodsSpuLogPage}
				searchSpan={6}
			>
				<SelectActions
					type="text"
					onClick={() => {
						handleCommand("查看日志");
					}}
				>
					<FileTextOutlined className="main-color" />
					查看日志
				</SelectActions>
			</ViewLogButton>
			{props.children}

			{!isSkuType && (
				<SelectActions
					type="ghost"
					onClick={() => {
						handleCommand("线上导入", {});
					}}
				>
					手动同步商品
				</SelectActions>
			)}
		</Space>
	);
};
export default forwardRef(RightBtns);
